using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace NodeCanvas
{
	[ExecuteInEditMode]
	[Category("✫ Utility")]
	public class ConditionList : ConditionTask
	{
		public bool allSuccessRequired = true;

		[SerializeField]
		private List<Object> conditions = new List<Object>();

		protected override string info
		{
			get
			{
				string text = ((conditions.Count == 0) ? "No Conditions" : string.Empty);
				if (conditions.Count > 1)
				{
					text = text + "<b>(" + ((!allSuccessRequired) ? "ANY True" : "ALL True") + ")</b>\n";
				}
				for (int i = 0; i < conditions.Count; i++)
				{
					if (!IsTrullyNull(conditions[i]))
					{
						ConditionTask conditionTask = conditions[i] as ConditionTask;
						if (conditionTask == null)
						{
							text = text + MissingTaskText(conditions[i]) + "\n";
						}
						else if (conditionTask.isActive)
						{
							text = text + conditionTask.summaryInfo + ((i != conditions.Count - 1) ? "\n" : string.Empty);
						}
					}
				}
				return text;
			}
		}

		protected override bool OnCheck()
		{
			int num = 0;
			foreach (ConditionTask item in conditions.OfType<ConditionTask>())
			{
				if (!item.isActive)
				{
					num++;
				}
				else if (item.CheckCondition(base.agent, base.blackboard))
				{
					if (!allSuccessRequired)
					{
						return true;
					}
					num++;
				}
				else if (allSuccessRequired)
				{
					return false;
				}
			}
			return num == conditions.Count;
		}

		protected override void OnGizmos()
		{
			foreach (ConditionTask item in conditions.OfType<ConditionTask>())
			{
				item.DrawGizmos();
			}
		}

		protected override void OnGizmosSelected()
		{
			foreach (ConditionTask item in conditions.OfType<ConditionTask>())
			{
				item.DrawGizmosSelected();
			}
		}

		private string MissingTaskText(Object o)
		{
			string text = ((!object.Equals(o, null)) ? o.ToString() : "NULL ENTRY");
			text = text.Replace(base.gameObject.name + " ", string.Empty);
			return string.Format("<color=#ff6457>* {0} *</color>", text);
		}

		private bool IsTrullyNull(Object o)
		{
			try
			{
				return o == null && o.GetType() != typeof(Object);
			}
			catch
			{
				return true;
			}
		}
	}
}
